/**
 * Copyright &copy; 2017 <a href="https://gitee.com/hackempire/emsite-parent">emsite</a> All rights reserved.
 */
package com.empire.emsite.modules.cms.service;

import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.empire.emsite.common.config.GlobalConstants;
import com.empire.emsite.common.persistence.Page;
import com.empire.emsite.common.service.TreeService;
import com.empire.emsite.modules.cms.dao.CategoryDao;
import com.empire.emsite.modules.cms.entity.Category;
import com.empire.emsite.modules.cms.entity.Site;
import com.empire.emsite.modules.sys.entity.Office;
import com.empire.emsite.modules.sys.entity.User;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

/**
 * 类CategoryService.java的实现描述：栏目Service
 * 
 * @author arron 2017年10月30日 下午4:10:07
 */
@Service
@Transactional(readOnly = true)
public class CategoryService extends TreeService<CategoryDao, Category> {

    public static final String CACHE_CATEGORY_LIST = "categoryList";

    private Category           entity              = new Category();

    @SuppressWarnings("unchecked")
    public List<Category> findByUser(boolean isCurrentSite, String module, User user, String currSiteId) {
        List<Category> list = null;
        if (list == null) {
            Category category = new Category();
            category.setOffice(new Office());
            category.getSqlMap().put("dsf", dataScopeFilter(user, "o", "u"));
            category.setSite(new Site());
            category.setParent(new Category());
            list = dao.findList(category);
            // 将没有父节点的节点，找到父节点
            Set<String> parentIdSet = Sets.newHashSet();
            for (Category e : list) {
                if (e.getParent() != null && StringUtils.isNotBlank(e.getParent().getId())) {
                    boolean isExistParent = false;
                    for (Category e2 : list) {
                        if (e.getParent().getId().equals(e2.getId())) {
                            isExistParent = true;
                            break;
                        }
                    }
                    if (!isExistParent) {
                        parentIdSet.add(e.getParent().getId());
                    }
                }
            }
            if (parentIdSet.size() > 0) {
                //FIXME 暂且注释，用于测试
                //				dc = dao.createDetachedCriteria();
                //				dc.add(Restrictions.in("id", parentIdSet));
                //				dc.add(Restrictions.eq("delFlag", Category.DEL_FLAG_NORMAL));
                //				dc.addOrder(Order.asc("site.id")).addOrder(Order.asc("sort"));
                //				list.addAll(0, dao.find(dc));
            }
        }

        if (isCurrentSite) {
            List<Category> categoryList = Lists.newArrayList();
            for (Category e : list) {
                if (Category.isRoot(e.getId())
                        || (e.getSite() != null && e.getSite().getId() != null && e.getSite().getId()
                                .equals(currSiteId))) {
                    if (StringUtils.isNotEmpty(module)) {
                        if (module.equals(e.getModule()) || "".equals(e.getModule())) {
                            categoryList.add(e);
                        }
                    } else {
                        categoryList.add(e);
                    }
                }
            }
            return categoryList;
        }
        return list;
    }

    public List<Category> findByParentId(String parentId, String siteId) {
        Category parent = new Category();
        parent.setId(parentId);
        entity.setParent(parent);
        Site site = new Site();
        site.setId(siteId);
        entity.setSite(site);
        return dao.findByParentIdAndSiteId(entity);
    }

    public Page<Category> find(Page<Category> page, Category category) {
        //		DetachedCriteria dc = dao.createDetachedCriteria();
        //		if (category.getSite()!=null && StringUtils.isNotBlank(category.getSite().getId())){
        //			dc.createAlias("site", "site");
        //			dc.add(Restrictions.eq("site.id", category.getSite().getId()));
        //		}
        //		if (category.getParent()!=null && StringUtils.isNotBlank(category.getParent().getId())){
        //			dc.createAlias("parent", "parent");
        //			dc.add(Restrictions.eq("parent.id", category.getParent().getId()));
        //		}
        //		if (StringUtils.isNotBlank(category.getInMenu()) && Category.SHOW.equals(category.getInMenu())){
        //			dc.add(Restrictions.eq("inMenu", category.getInMenu()));
        //		}
        //		dc.add(Restrictions.eq(Category.FIELD_DEL_FLAG, Category.DEL_FLAG_NORMAL));
        //		dc.addOrder(Order.asc("site.id")).addOrder(Order.asc("sort"));
        //		return dao.find(page, dc);
        //		page.setSpringPage(dao.findByParentId(category.getParent().getId(), page.getSpringPage()));
        //		return page;
        category.setPage(page);
        category.setInMenu(GlobalConstants.ShowHideEnum.SHOW.getValue());
        page.setList(dao.findModule(category));
        return page;
    }

    @Override
    @Transactional(readOnly = false)
    public void save(Category category) {
        if (StringUtils.isNotBlank(category.getViewConfig())) {
            category.setViewConfig(StringEscapeUtils.unescapeHtml4(category.getViewConfig()));
        }
        super.save(category);
        //UserUtils.removeCache(CACHE_CATEGORY_LIST);
    }

    @Override
    @Transactional(readOnly = false)
    public void delete(Category category) {
        super.delete(category);
        //UserUtils.removeCache(CACHE_CATEGORY_LIST);
    }

    /**
     * 通过编号获取栏目列表
     */
    public List<Category> findByIds(String ids) {
        List<Category> list = Lists.newArrayList();
        String[] idss = StringUtils.split(ids, ",");
        if (idss.length > 0) {
            //			List<Category> l = dao.findByIdIn(idss);
            //			for (String id : idss){
            //				for (Category e : l){
            //					if (e.getId().equals(id)){
            //						list.add(e);
            //						break;
            //					}
            //				}
            //			}
            for (String id : idss) {
                Category e = dao.get(id);
                if (null != e) {
                    //System.out.println("e.id:"+e.getId()+",e.name:"+e.getName());
                    list.add(e);
                }
                //list.add(dao.get(id));

            }
        }
        return list;
    }

}
